// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: null,
    lists: wx.getStorageSync('lists') || [],
    isUp: true,
    isShengxu: true,
    isSlice: true,
    last_ep_id: null,
    last_ep: null,
    acount: null
  },

  add(event) {
    this.setData({
      lists: [...this.data.lists, event.currentTarget.dataset.id]
    })
    wx.setStorageSync('lists', this.data.lists)
  },

  remove(event) {
    this.setData({
      lists: this.data.lists.filter(o => o !== event.currentTarget.dataset.id)
    })
    wx.setStorageSync('lists', this.data.lists)
  },

  changeUp() {
    this.setData({
      isUp: !this.data.isUp
    })
  },

  changeSheng() {
    this.setData({
      isShengxu: !this.data.isShengxu
    })
  },

  changeIsSlice() {
    this.setData({
      isSlice: false
    })
  },

  navToRead(event) {
    wx.navigateTo({
      url: `/pages/read/read?comic_id=${event.currentTarget.dataset.comic_id}&ep_id=${event.currentTarget.dataset.ep_id}&comic_title=${this.data.detail.title}&ep_title=${event.currentTarget.dataset.ep_title}`,
    })
  },

  navToDetail(event) {
    wx.navigateTo({
      url: `/pages/detail/detail?id=${event.currentTarget.dataset.id}&title=${event.currentTarget.dataset.title}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })

    this.setData({
      comic_id: options.id,
      last_ep_id: wx.getStorageSync('history')[options.id]
    })

    wx.showLoading({
      title: '加载中',
    })

    wx.request({
      url: 'https://apis.netstart.cn/mbcomic/ComicDetail?',
      data: {
        comic_id: options.id
      },
      success: (res) => {
        // console.log(res.data.data.ep_list);
        this.setData({
          detail: res.data.data,
          last_ep: res.data.data.ep_list.find(o => o.id == this.data.last_ep_id)
        })
      },
      complete: () => {}
    })

    wx.request({
      url: 'https://apis.netstart.cn/mbcomic/reply?',
      data: {
        oid: options.id
      },
      success: (res) => {
        this.setData({
          comment: res.data.data,
          acount: res.data.data.page.acount,
        })
      },
      complete: () => {}
    })

    wx.request({
      url: 'https://apis.netstart.cn/mbcomic/Recommend?',
      data: {
        comic_id: options.id
      },
      success: (res) => {
        // console.log(res);
        this.setData({
          manga: res.data.data
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.detail) {
      this.setData({
        last_ep_id: wx.getStorageSync('history')[this.data.comic_id],
      })
      this.setData({
        last_ep: this.data.detail.ep_list.find(o => o.id == this.data.last_ep_id)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})